package org.example.proxy;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Mock服务代理（JDK动态代理）
 *
 * */
@Slf4j
public class MockServiceProxy implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 根据方法的返回值类型，返回特定的默认对象
        Class<?> returnType = method.getReturnType();
        log.info("MockServiceProxy invoke method: {}, args: {}", method.getName(), args);
        return getDefaultObject(returnType);
    }

    private Object getDefaultObject(Class<?> returnType) {
        if(returnType.isPrimitive()) {
            if(returnType == int.class) {
                return 0;
            } else if(returnType == long.class) {
                return 0L;
            } else if(returnType == double.class) {
                return 0.0;
            } else if(returnType == float.class) {
                return 0.0f;
            } else if(returnType == boolean.class) {
                return false;
            } else if(returnType == char.class) {
                return '\u0000';
            } else if(returnType == byte.class) {
                return (byte) 0;
            } else if(returnType == short.class) {
                return (short) 0;
            }
        }
        // 返回对象类型
        return null;
    }
}
